package a10_动态规划;

/**
 * <p>
 * </p>
 *
 * @author flyduck
 * @since 2025/2/18
 */
public class a29_打家劫舍 {
    //dp[i]:包含下标i能偷的最大钱币为dp[i]
    //递推公式：dp[i] = Math.max(nums[i] + dp[i-2],dp[i-1])
    //偷i:nums[i] + dp[i-2]
    //不偷i:dp[i-1]
    //初始化：dp[0]=nums[0] dp[1] = Math.max(nums[0],nums[1])
    public int rob(int[] nums) {
        if(nums.length < 2){
            return nums[0];
        }
        int[] dp = new int[nums.length];
        dp[0] = nums[0];
        dp[1] = Math.max(nums[0],nums[1]);

        for (int i = 0; i < nums.length; i++) {
            dp[i] = Math.max(nums[i] + dp[i-2],dp[i-1]);
        }
        return dp[dp.length-1];
    }
}
